﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;

namespace MINERP.DAL.PrintManage
{
    public class PaperDAL:DALBase
    {
        public static List<PaperInfo> GetPaperInfos(int printerId)
        {
            List<PaperInfo> listPaperInfo = new List<PaperInfo>();
            using (newSampleYPDBEntities Entities = new newSampleYPDBEntities())
            {
                printer printer = Entities.printer.FirstOrDefault(p => p.PrinterID == printerId);
                if (printer != null)
                {
                    printer.PaperInfo.Load();
                    //listPaperInfo = Entities.PaperInfo.Where("it.Status <>2 and it.PrinterID =" + printerId).ToList();
                    //listPaperInfo = printer.PaperInfo.ToList();
                    foreach (var item in printer.PaperInfo)
                    {
                        if (item.Status != 2)
                        {
                            listPaperInfo.Add(item);
                        }
                    }
                    foreach (var item in printer.PaperInfo)
                    {
                        if (!item.paperFaceReference.IsLoaded)
                        {
                            item.paperFaceReference.Load();
                        }
                        if (!item.paperTypeReference.IsLoaded)
                        {
                            item.paperTypeReference.Load();
                        }
                    }
                }
            }
            return listPaperInfo;
        }

        public static PaperInfo GetPaperInfoObj(int id)
        {
            PaperInfo paperInfo = null;
            using (newSampleYPDBEntities Entities = new newSampleYPDBEntities())
            {
                try
                {
                    paperInfo = Entities.PaperInfo.First(p => p.PrintPaperId == id);
                    if (!paperInfo.paperTypeReference.IsLoaded)
                    {
                        paperInfo.paperTypeReference.Load();
                    }
                    if (!paperInfo.paperFaceReference.IsLoaded)
                    {
                        paperInfo.paperFaceReference.Load();
                    }
                }
                catch(Exception ex)
                {
                    throw ex;
                }
            }
            return paperInfo;
        }
        //获取正在打印的纸
        public static PaperInfo GetPaperInfoUsingObj(int printerid)
        {
            PaperInfo paperInfo = null;
            using (newSampleYPDBEntities Entities = new newSampleYPDBEntities())
            {
                try
                {
                    paperInfo = Entities.PaperInfo.FirstOrDefault(p => p.Status == 1&&p.printer.PrinterID==printerid);
                    if (!paperInfo.paperTypeReference.IsLoaded)
                    {
                        paperInfo.paperTypeReference.Load();
                    }
                    if (!paperInfo.paperFaceReference.IsLoaded)
                    {
                        paperInfo.paperFaceReference.Load(); 
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            return paperInfo;
        }

        public static void Add(PaperInfo obj)
        {
            DataEntites.AddToPaperInfo(obj);
            DataEntites.SaveChanges();
        }

        public static void DeleteObj(int id)
        {
            using (newSampleYPDBEntities Entities = new newSampleYPDBEntities())
            {
                try
                {
                    PaperInfo pi = Entities.PaperInfo.First(p => p.PrintPaperId == id);
                    pi.Status = 2;
                    Entities.SaveChanges();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
        public static void Update()
        {
            DataEntites.SaveChanges();
        }
    }
}
